package cn.stylefeng.guns.modular.api.tool;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.stylefeng.guns.sys.modular.consts.entity.SysConfig;
import cn.stylefeng.guns.sys.modular.consts.service.SysConfigService;
import cn.stylefeng.roses.core.util.ToolUtil;

@Component
public class WxTool {
	
	@Autowired
    SysConfigService sysConfigService;
	
	private static SysConfigService ssysConfigServices;
	private static String appId = "wx62288580e267fca0";//AppId
	private static String appSecret = "f59cd8892fc18fd937b2b481ab9d2414";//appSecret
	private static String loginUrl = "https://api.weixin.qq.com/sns/jscode2session";//登录地址
	private static String atUrl = "https://api.weixin.qq.com/cgi-bin/token";//access_token
	private static String phoneUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";//获取手机号
	private static String domUrl = "https://api.weixin.qq.com/wxa/getwxadevinfo";//获取域名配置
	
	@PostConstruct
    public void init() {
		ssysConfigServices = this.sysConfigService;
    }

	/**
	 * 获取登录信息
	 *
	 */
	public static Map<String, String> getLoginInfo(String code) {
		
		Map<String, String> map = new HashMap<String, String>();
		
		String params = "appid=" + appId + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=authorization_code";
		String sr = HttpRequest.sendGet(loginUrl, params);
		//解析相应内容（转换成json对象）
	    JSONObject json = JSONObject.parseObject(sr);
	    //用户的唯一标识（openid）
	    String openid = (String) json.get("openid");
	    
	    String unionid = "";
	    if(json.containsKey("unionid")) {
	    	unionid = (String) json.get("unionid");
	    }
	    
	    map.put("unionid", unionid);
	    map.put("openid", openid);
	    
		return map;
	}

	/**
	 * 获取手机号码
	 *
	 */
	public static String getPhoneNum(String code) {
		String access_token = getac();
		
		JSONObject json = new JSONObject();
		json.put("code", code);
		String jsonstr = JSONObject.toJSONString(json);
		String ret = HttpRequest.sendPost(phoneUrl+"?access_token=" + access_token, jsonstr);
		
		JSONObject res = JSONObject.parseObject(ret);
		
		Integer errcode = res.getInteger("errcode");
		if(errcode==0) {
			JSONObject phoneinfo = res.getJSONObject("phone_info");
			String phone = phoneinfo.getString("phoneNumber");
			return phone;
		}else {
			return null;
		}
	}
	
	/**
	 * 获取AccessToken
	 *
	 */
	public static String getAccessToken() {
		String params = "appid=" +appId + "&secret=" + appSecret + "&grant_type=client_credential";
		String sr = HttpRequest.sendGet(atUrl, params);
		JSONObject json = JSONObject.parseObject(sr);
		String access_token = json.getString("access_token");
		return access_token;
	}
	
	public static String getac() {
		
		SysConfig sc = ssysConfigServices.getOne(new QueryWrapper<SysConfig>()
        		.eq("code", "WXTOKEN"));
		
		if(ToolUtil.isEmpty(sc.getValue())) {
			sc.setValue(getAccessToken());
			ssysConfigServices.updateById(sc);
		}
		
		String params = "access_token=" +sc.getValue();
		String di = HttpRequest.sendGet(domUrl, params);
		JSONObject json = JSONObject.parseObject(di);
		Integer errcode = json.getInteger("errcode");
		
		if(errcode != 0) {
			sc.setValue(getAccessToken());
			ssysConfigServices.updateById(sc);
		}
		
		
		return sc.getValue();
	}
	
	
}
